package com.mall.service.marketing.controller;

import com.mall.commons.common.CommonResult;
import com.mall.model.domain.SmsHomeBrand;
import com.mall.model.vo.PageResult;
import com.mall.service.marketing.service.SmsHomeBrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import java.util.List;

@RestController
@RequestMapping("/home/brand")
public class SmsHomeBrandController {

    @Autowired
    private SmsHomeBrandService smsHomeBrandService;

    /**
     * 分页查询推荐品牌
     * @param brandName
     * @param recommendStatus
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("/list")
    public CommonResult list(String brandName, Integer recommendStatus,
                             @RequestParam(defaultValue = "5") Integer pageSize,
                             @RequestParam(defaultValue = "1") Integer pageNum){
        PageResult pr = smsHomeBrandService.findList(brandName,recommendStatus,pageNum,pageSize);
        return CommonResult.success(pr);
    }

    /**
     * 添加首页推荐品牌
     * @param smsHomeBrand
     * @return
     */
    @PostMapping("/create")
    public CommonResult save(@RequestBody SmsHomeBrand smsHomeBrand){
        smsHomeBrandService.save(smsHomeBrand);
        return CommonResult.success("添加成功");
    }

    /**
     * 批量删除推荐品牌
     * @param ids
     * @return
     */
    @PostMapping("/delete")
    public CommonResult delete(@RequestParam List<Integer> ids){
        smsHomeBrandService.removeByIds(ids);
        return CommonResult.success("删除成功");
    }

    /**
     * 批量修改推荐品牌状态
     * @param ids
     * @param recommendStatus
     * @return
     */
    @PostMapping("/update/recommendStatus")
    public CommonResult update(@RequestParam List<Integer> ids,@RequestParam Integer recommendStatus){
        System.err.println(ids + "==" + recommendStatus);

        for (Integer id : ids) {
            SmsHomeBrand smsHomeBrand = smsHomeBrandService.getById(id);
            smsHomeBrand.setRecommendStatus(recommendStatus);
            smsHomeBrandService.updateById(smsHomeBrand);
        }
        return CommonResult.success("修改状态成功");
    }

    /**
     * 修改推荐品牌排序
     * @param sort
     * @param id
     * @return
     */
    @PostMapping("/update/sort/{id}")
    public CommonResult updateSort(@RequestParam Integer sort,@PathVariable Integer id){
        SmsHomeBrand smsHomeBrand = smsHomeBrandService.getById(id);
        smsHomeBrand.setSort(sort);
        smsHomeBrandService.updateById(smsHomeBrand);

        return CommonResult.success("修改排序成功");
    }

}
